/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.sapra.abc;

import java.util.List;
import nextapp.echo2.app.Alignment;
import nextapp.echo2.app.Button;
import nextapp.echo2.app.Color;
import nextapp.echo2.app.Column;
import nextapp.echo2.app.Extent;
import nextapp.echo2.app.Insets;
import nextapp.echo2.app.Row;
import nextapp.echo2.app.SelectField;
import nextapp.echo2.app.TextField;
import nextapp.echo2.app.WindowPane;
import nextapp.echo2.app.event.ActionEvent;
import nextapp.echo2.app.event.ActionListener;
import nextapp.echo2.app.list.DefaultListModel;
import nextapp.echo2.extras.app.AccordionPane;
import nextapp.echo2.extras.app.layout.AccordionPaneLayoutData;
import org.sapra.dao.Carrera;
import org.sapra.echo.Helpers;
import org.sapra.echo.wAviso;
import org.sapra.main.vPrincipal;

/**
 *
 * @author cobain
 */
public class vCarrera extends nextapp.echo2.app.ContentPane {

    //INICIO
    Column mParent=null;
    private vPrincipal princ;
    List<Carrera> car;
    Carrera mCar;
    public final static int EDITAR = 2;
    public final static int NUEVO = 1;
    public final static int BORRAR = 1;
    public static int accion;

    //COMPONENTES
    private Button bNuevo, bEditar, bBorrar, bGuardar, bCancelar, bConfirmar;
    private WindowPane ven;
    private TextField nom_carrera;
    private SelectField lista_car;
    private Row row3, row4, row5, row6, row7;

    
    public vCarrera(vPrincipal princ) {
        super();
        this.princ=princ;
        initComponents();
    }

    public vCarrera(Column parent){
        mParent = parent;
        initComponents();
    }
    
    private void initComponents(){
            Column col1;


        if(mParent != null) col1 = mParent;
        else{
            col1 = Helpers.Column("Default", new Extent(5),this);
            Row x=princ.getMenu();
            x.setAlignment(Alignment.ALIGN_CENTER);
            col1.add(x);
        }

            //ENCABEZADOS
            Row row1=Helpers.Row("Default", new Extent(5), col1);
            Helpers.Label("CATALOGO CARRERA.", new Color(0x011dac), row1);
            row1.setAlignment(Alignment.ALIGN_CENTER);

            Row row2=Helpers.Row("Default", new Extent(5), col1);
            Helpers.Label("SELECCIONE UNA ACCION.", Color.BLACK, row2);
            row2.setAlignment(Alignment.ALIGN_CENTER);

            //BOTONES DE ACCION
            bNuevo=Helpers.Button2("NUEVO",  new ActionListener(){public void actionPerformed(ActionEvent e) {menuNuevo(e);}});
            bEditar=Helpers.Button2("EDITAR", new ActionListener(){public void actionPerformed(ActionEvent e) {menuEditar(e);}});
            bBorrar=Helpers.Button2("BORRAR", new ActionListener(){public void actionPerformed(ActionEvent e) {menuBorrar(e);}});


          Column col_b=new Column();

           col_b.add(bNuevo);
           col_b.add(bEditar);
           col_b.add(bBorrar);

           col_b.setCellSpacing(new Extent(5));
           col_b.setInsets(new Insets(new Extent(10)));

           AccordionPane ac=new AccordionPane();
           AccordionPaneLayoutData layoutdata=new AccordionPaneLayoutData();
           layoutdata.setTitle("OPCIONES");

           col_b.setLayoutData(layoutdata);
           AccordionPaneLayoutData layoutData2 = new AccordionPaneLayoutData();
           ac.add(col_b);

          ven=Helpers.Window("MENU CARRERA");
          ven.setHeight(new Extent(185));
          ven.setWidth(new Extent(185));
          ven.setPositionY(new Extent(60));
          ven.setPositionX(new Extent(60));
          ven.setClosable(false);
          ven.setMovable(false);
          ven.add(ac);
          add(ven);

          //DATOS
          row3=Helpers.Row("Default", new Extent(5), col1);
          lista_car=Helpers.SelectField(row3, new ActionListener(){public void actionPerformed(ActionEvent e) {Seleccion(e);}});
          lista_car.setWidth(new Extent(450));
          row3.setAlignment(Alignment.ALIGN_CENTER);

          row4=Helpers.Row("Default", new Extent(10), col1);
          Helpers.Label("NOMBRE CARRRERA:", Color.BLACK, row4);
          nom_carrera=Helpers.Text("", row4);
          row4.setAlignment(Alignment.ALIGN_CENTER);

            row5=Helpers.Row("Default", new Extent(5), col1);
            bGuardar=Helpers.Button("GUARDAR", row5, new ActionListener(){public void actionPerformed(ActionEvent e) {menuGuardar(e);}});
            bCancelar=Helpers.Button("CANCELAR", row5, new ActionListener(){public void actionPerformed(ActionEvent e) {menuCancelar(e);}});
            row5.setAlignment(Alignment.ALIGN_CENTER);

            row6=Helpers.Row("Default", new Extent(5), col1);
            bConfirmar=Helpers.Button("CONFIRMAR BORRAR", row6, new ActionListener(){public void actionPerformed(ActionEvent e) {confirmarBorrar(e);}});
            bCancelar=Helpers.Button("CANCELAR", row6, new ActionListener(){public void actionPerformed(ActionEvent e) {menuCancelar(e);}});
            row6.setAlignment(Alignment.ALIGN_CENTER);

            FirstTime();

     }

     void FirstTime(){

        UpdateView();

       }

     void UpdateView(){
        List<Carrera> car= dCarrera.getCarreras(0, null);
        lista_car.setModel(new DefaultListModel(car.toArray()));
        mCar= null;
        row3.setVisible(false);
        row4.setVisible(false);
        row5.setVisible(false);
        row6.setVisible(false);

       }
     void Seleccion(ActionEvent e){
        mCar = (Carrera) lista_car.getSelectedItem();
        if(mCar == null) return;

       }

    void menuNuevo(ActionEvent e){
        row4.setVisible(true);
        row5.setVisible(true);
        accion=NUEVO;

        }

    void menuEditar(ActionEvent e){
        row4.setVisible(true);
        row5.setVisible(true);
        row3.setVisible(true);
        //row7.setVisible(true);
        accion=EDITAR;

        }

       void menuBorrar(ActionEvent e){
        row3.setVisible(true);
        row6.setVisible(true);
        accion=BORRAR;

        }

       void menuCancelar(ActionEvent e){
            ocultarFilas();
        }

       public void CleanFields(){
            nom_carrera.setText("");
        }

        public void ocultarFilas(){
                        if(accion==NUEVO){
        row4.setVisible(false);
        row5.setVisible(false);
            }
            if(accion==EDITAR){
        row3.setVisible(false);
        row4.setVisible(false);
        row5.setVisible(false);
        //row7.setVisible(false);
            }
            if(accion==BORRAR){
        row3.setVisible(false);
        row6.setVisible(false);

            }
        }

       void menuGuardar(ActionEvent e){
    int val2;
        String msj=null;
        msj=Validar();
        if(msj!=null){
             add(new wAviso("Error", msj));
             return ;
        }

            if(accion==NUEVO){
            String nombre = nom_carrera.getText();
            dCarrera.NuevoCarrera(nombre);
            add(new wAviso("Exito!!!","CARRERA AGREGADA CORRECTAMENTE"));
            }

          if(accion==EDITAR){
           mCar = (Carrera) lista_car.getSelectedItem();
           nom_carrera.setText(mCar.getNombre());
           if(mCar==null){
               add(new wAviso("Error", "NO HAY SELECCION"));
           }
          else{
           String nombre=nom_carrera.getText();
           dCarrera.editarCarrera(mCar, nombre);
           add(new wAviso("Exito!!!","CARRERA MODIFICADA CORRECTAMENTE"));
          }
        }
        CleanFields();
        UpdateView();
    }

      public void confirmarBorrar(ActionEvent e){
         mCar = (Carrera) lista_car.getSelectedItem();
        if(mCar == null)return;
        if (dCarrera.borrarCarrera(mCar) == 0){
            add(new wAviso("ERROR","NO SE PUEDE BORRAR EL REGISTRO SELECCIONADO"));
        }
        else{
             add(new wAviso("Exito!!!","CARRERA BORRADA CORRECTAMENTE"));
        }
         UpdateView();
        }

        private String Validar(){

        String msj1="Formato Erroneo para ";
        String msj3="Favor de Llenar el campo";
        boolean b,c;
        b=isNumeric(nom_carrera.getText());
        if(nom_carrera.getText().trim().equals(""))return msj3+" NOMBRE";
        if(b==true)return msj1+" NOMBRE";
        if(accion==NUEVO){
             List<Carrera> carr=dCarrera.getCarreras(1, nom_carrera.getText().trim());
              if(carr.size()>0)return " CARRERA YA EXISTE";
         }

            return null;
        }

        //VALIDAR CADENA DEL CAMPO DE TEXTO
         private  boolean isNumeric(String cadena){
         cadena=nom_carrera.getText();
         try {
              Integer.parseInt(cadena);
              return true;
             }
         catch (NumberFormatException nfe)
         {
           return false;

         }

      }
       /*private boolean validarNombre(String cadena){
             //cadena=nom_universidad.getText();
             List<Universidad> univs2= dUniversidad.getUniversidades();
             for(int i=0;i<univs2.size();i++)
             {
                 if(univs.get(i).toString().equals(cadena));
                 return true;
             }
             return false;
         }*/


}
